package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.awt.image.BandCombineOp;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-04-28
 * Time: 11:22
 */
/*
用户详情页测试
 */
public class UserInfoPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public UserInfoPage() {
        super(url);
    }

    // 进入个人中心测试
    public void UserInfoEnter() throws InterruptedException {
        // 需要重新登录
        // 返回登录页并刷新页面
        driver.navigate().refresh();
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 点击 "用户头像" 按钮, 进入选择进入用户个人中心界面
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();
        // 点击 "个人中心" 按钮, 进入个人中心
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        // 获取界面元素
        WebElement ele1 = driver.findElement(By.cssSelector("#settings_submit_password"));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele1));
        String ele2 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col-3.d-none.d-md-block.border-end > div > div > a")).getText();
        // 断言判断是否成功进入
        assert ele2.equals("我的账户");
    }

    // 昵称, 邮箱地址, 电话号, 个人介绍 修改后都找不到可以对比的值, 人工测试都正常, 就不加入自动化脚本中了
    // 密码修改成功测试
    public void PasswordChange() {
        // 找到原密码输入框
        WebElement ele3 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        // 点击原密码输入框
        Actions actions = new Actions(driver);
        actions.click(ele3).perform();
        // 输入原密码
        actions.sendKeys("123").perform();
        // 找到新原密码输入框
        WebElement ele4 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        // 点击新原密码输入框
        actions.click(ele4).perform();
        // 输入新原密码
        actions.sendKeys("123").perform();
        // 找到确认密码输入框
        WebElement ele5 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        // 点击确认密码输入框
        actions.click(ele5).perform();
        // 输入确认密码
        actions.sendKeys("123").perform();
        // 点击修改密码按钮
        WebElement ele6 = driver.findElement(By.cssSelector("#settings_submit_password"));
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele6));
        actions.click(ele6).perform();
        // 断言检查是否修改成功 (修改成功后跳转到登录页面)
        wait.until(ExpectedConditions.urlToBe("http://127.0.0.1:58080/sign-in.html"));
        String ele7 = driver.getCurrentUrl();
        assert ele7.equals("http://127.0.0.1:58080/sign-in.html");
    }

    // 密码修改失败 (原密码输入错误)
    public void PasswordChangeFail1() {
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 点击 "用户头像" 按钮, 进入选择进入用户个人中心界面
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();
        // 点击 "个人中心" 按钮, 进入个人中心
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        // 找到原密码输入框
        WebElement ele8 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        // 点击原密码输入框
        Actions actions = new Actions(driver);
        actions.click(ele8).perform();
        // 输入原密码
        actions.sendKeys("1234").perform();
        // 找到新原密码输入框
        WebElement ele9 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        // 点击新原密码输入框
        actions.click(ele9).perform();
        // 输入新原密码
        actions.sendKeys("123").perform();
        // 找到确认密码输入框
        WebElement ele10 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        // 点击确认密码输入框
        actions.click(ele10).perform();
        // 输入确认密码
        actions.sendKeys("123").perform();
        // 点击修改密码按钮
        WebElement ele11 = driver.findElement(By.cssSelector("#settings_submit_password"));
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele11));
        actions.click(ele11).perform();
        // 通过获取界面 url 来判断修改是否成功
        String ele12 = driver.getCurrentUrl();
        assert ele12.equals("http://127.0.0.1:58080/index.html");
    }

    // 密码修改失败 (确认密码错误)
    public void PassWordChangeFail2() {
        // 找到原密码输入框
        WebElement ele13 = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        // 点击原密码输入框
        Actions actions = new Actions(driver);
        actions.click(ele13).perform();
        // 输入原密码
        actions.sendKeys("123").perform();
        // 找到新原密码输入框
        WebElement ele14 = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        // 点击新原密码输入框
        actions.click(ele14).perform();
        // 输入新原密码
        actions.sendKeys("123").perform();
        // 找到确认密码输入框
        WebElement ele15 = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        // 点击确认密码输入框
        actions.click(ele15).perform();
        // 输入确认密码
        actions.sendKeys("1234").perform();
        // 点击修改密码按钮
        WebElement ele16 = driver.findElement(By.cssSelector("#settings_submit_password"));
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele16));
        actions.click(ele16).perform();
        // 通过获取界面 url 来判断修改是否成功
        String ele17 = driver.getCurrentUrl();
        assert ele17.equals("http://127.0.0.1:58080/index.html");
    }
}
